#!/usr/bin/env python
# _*_coding:utf-8_*_

"""
@Time :    2019/10/6 17:17
@Author:  ThinHeader
@File: custom_exceptions.py
@Software: PyCharm
"""
from functools import wraps

from django.http import JsonResponse
from django.shortcuts import render


class UserException(Exception):
    """
    用户端异常
    """
    pass


def decorator_exception(func):
    @wraps(func)
    def __wrapper__(request, *args, **kwargs):
        try:
            return func(request, *args, **kwargs)
        except UserException as e:
            if request.method == 'GET':
                return render(request, 'mobile/message.html', {'message': str(e)})
            return JsonResponse({'status': 1, 'detail': str(e)})

    return __wrapper__
